package com.hspedu.web.homework;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;

@Controller
public class FileDownLoad {
    @RequestMapping("/downLoad")

    public ResponseEntity<byte[]> download(HttpSession session) throws IOException {
        InputStream resourceAsStream = session.getServletContext().getResourceAsStream("/img/1.png");
        byte[] bytes = new byte[resourceAsStream.available()];
        resourceAsStream.read(bytes);
        HttpStatus ok = HttpStatus.OK;
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Disposition","attachement;filename=1.png");
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, httpHeaders,ok);
        return responseEntity;
    }
}
